var check=-1;
var gcid="";
var addedMark="";
var likeName="";
var pageNo=1;
var pageSize=10;
var flag=0;
var applyStatus="";
var giids=[];
var pageNum="";

showList();
/////商品类型
$("document").ready(function(){
    ajax({
        url:"goods/goodsCategories",
        data:{},
        success:function(data){
            
            var html=template("template",data);
            document.getElementById('goodsClass').innerHTML = html;
        }
    });
    keyEnter(serchResult)
});

function serchResult() {
    if(flag!==0){
        return;
    }
    pageNo = 1;
    showList();
}
//table 的ajax请求
function showList(){
    if(flag!==0){
        return;
    }
    flag=1;
    likeName=$("#searchName").val();
    ajax({
        url:"goods/goodsData",
        data:{
            //商品品类大类id
            gcid:gcid,
            //上架状态
            addedMark:addedMark,
            //搜索关键字
            likeName:likeName,
            //第几页
            pageNo:pageNo,
            //页面大小
            pageSize:pageSize,
            //请求状态
            requestStatus:check
        },
        success:function(data){
            if(data.success){
                
                flag=0;
                temp(data);
                createPageInfo(data.data.goodsDataList);
                $("#checkAll").html("全选");
                //删除当前，移至回收站
                $(".delete").on("click",function(){
                    var giid=$(this.parentNode.parentNode).attr("data-giid");
                    ajax({
                        url:"goods/updateGoodsFlag",
                        data:{
                            giid:giid,
                            flagStatus:0
                        },
                        success:function(data){
                            
                            showList();
                        }
                    })
                });
                $(".restore").on("click",function(){
                    var giid=$(this.parentNode.parentNode).attr("data-giid");
                    ajax({
                        url:"goods/updateGoodsFlag",
                        data:{
                            giid:giid,
                            flagStatus:1
                        },
                        success:function(data){
                            
                            showList();
                        }
                    })
                });
                $(".edit").on("click",function(){
                    var biid=$(this.parentNode.parentNode).attr("data-biid");
                    var giid=$(this.parentNode.parentNode).attr("data-giid");
                    var msg={"biid":biid,"handle":"edit","giid":giid};
                    setStorJson("jumpToGoodsAdd",msg);
                    window.location.href="goodsAdd.html"
                });
                $(".copy").on("click",function(){
                    var biid=$(this.parentNode.parentNode).attr("data-biid");
                    var giid=$(this.parentNode.parentNode).attr("data-giid");
                    var msg={"biid":biid,"handle":"copy","giid":giid};
                    setStorJson("jumpToGoodsAdd",msg);
                    window.location.href="goodsAdd.html"
                })
            }else{
                flag=0;
                layer.msg(data.message)
            }
        }
    });
}

//请求成功后执行的table列表
function temp(data){
    var count=data.data.goodsDataAmount;
    $('#total').html(count.total);
    $('#passed').html(count.passed);
    $('#checkPending').html(count.checkPending);
    $('#unPassed').html(count.unPassed);
    $('#trash').html(count.trash);
    var goodsData=data.data.goodsDataList;
    var html=template("goodsTemplate",goodsData);
    document.getElementById('tbody').innerHTML = html;
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
    previewImg();
}

//批量操作
$('#apply').click(function(){
    if(applyStatus===""){
        layer.msg("该选项禁止选择");
    }else{
        choose();
        if(giids.length==0){
            layer.msg("请选择需要操作的商品");
            return;
        }
        ajax({
            url:"goods/batchOperation",
            data:{
                type:applyStatus,
                giids:giids
            },
            success:function(data){
                
                layer.msg(data.msg);
                showList();
            }
        });
    }
});
$("#trans").click(function(){
    $("#conditions").slideToggle();
});
//修改的商品集
function choose(){
    giids=[];
    var checked=$('.i-checks').children();
    for(var i=0;i<$(checked).length;i++){
        if($($(checked)[i]).hasClass("checked")&&$(checked)[i].parentNode.parentNode.nextElementSibling!==null){
            giids.push($(checked)[i].parentNode.parentNode.nextElementSibling.innerHTML);
        }
    }
}

$("#signing").click(function(){
    $("#handle").val("1").trigger("change");
    $("#apply").trigger("click");
});
$("#notSigning").click(function(){
    $("#handle").val("0").trigger("change");
    $("#apply").trigger("click");
});
$("#pass").click(function(){
    $("#handle").val("2").trigger("change");
    $("#apply").trigger("click");
});
$("#unpass").click(function(){
    $("#handle").val("3").trigger("change");
    $("#apply").trigger("click");
});
//批量操作
$('#handle').change(function(){
    applyStatus=$(this).val();
});
//全选/全不选
$("#checkAll").click(function () {
    if ($(this).text() == "全选") {
        $('.i-checks').iCheck('check');
        $(this).text("不选")
    } else {
        $('.i-checks').iCheck('uncheck');
        $(this).text("全选")
    }
});
$("#triggerCheck").on("ifClicked",function(){
    $("#checkAll").trigger("click");
});

//按键颜色
$(".btnStyle").click(function(){
    $(".btnStyle").removeClass("btnColor");
    $(this).addClass("btnColor");
});

////审核状态
$('.verify>a').on("click",function(){
    if($(this).hasClass("total")){
        check=-1;
    } else if($(this).hasClass("passed")){
        check=2;
    }  else if($(this).hasClass("checkPending")){
        check=0;
    } else if($(this).hasClass("unPassed")){
        check=3;
    } else if($(this).hasClass("trash")){
        check=4;
    }
    pageNo=1;
    showList();
});
$("#goodsClass").change(function(){
    gcid=$(this).val();
    pageNo=1;
    showList();
});
$("#shelfStatus").change(function(){
    addedMark=$(this).val();
    pageNo=1;
    showList();
});
$('#search').click(function(){
    var value=$('#searchName').val();
    if(value==""){
        alert("搜索不能为空")
    }else{
        likeName=value;
        pageNo=1;
        showList();
    }
});
//改变页面显示数量
$("#pageSizeInfo").change(function () {
    $("#pageSizeInfo").blur();
    if (flag == 1) {
        return
    }
    pageSize = this.options[this.options.selectedIndex].value;
    pageNo=1;
    showList();
});

//创建页码信息
function createPageInfo(data) {
    $("#startRow").text(data.startRow);
    $("#endRow").text(data.endRow);
    $("#totalPage").text(data.total);
    pageNum=data.pages;
    var temp = '';
    for (var i = 1; i < data.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageInfo").html(temp)
    .val(data.pageNum)
    .change(function () {
        $("#pageInfor").blur(); 
        if (flag == 1) {
            return
        }
        pageNo = this.options[this.options.selectedIndex].value;
        showList();
    });
    $("#firstPage").click(function () {
        if (pageNo!= 1) {
            if (flag == 1) {
                return
            }
            pageNo = 1;
            showList();
        }
    });
    $("#lastPage").click(function () {
        if (pageNo < pageNum) {
            if (flag == 1) {
                return
            }
            pageNo = pageNum;
            showList();
        }
    });
    $("#nextPage").click(function () {
        if (pageNo < pageNum) {
            if (flag == 1) {
                return
            }
            pageNo++;
            showList();
        }
    });
    $("#previousPage").click(function () {
        if (pageNo > 1) {
            if (flag == 1) {
                return
            }
            pageNo--;
            showList();
        }
    });
    $("#pageSizeInfo").change(function () {
        $("#pageSizeInfo").blur()
        if (flag == 1) {
            return
        }
        pageSize = this.options[this.options.selectedIndex].value;
        showList();
    });

}